<?php

/**
 * 用户
 *
 * @author cdlei
 */

require_once 'CommonController.php';

class Admin_UserSessionController extends Admin_CommonController
{
    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->_model = new Application_Model_UserSession();
        $this->view->title = "管理中心";
    }

    /**
     * 默认动作
     * 
     * @return void
     */
    public function indexAction()
    {
        $partnerModel = new Application_Model_Partner();
        $userModel = new Application_Model_User();
        $time = time();

        //获取当前页码
        $pageSize = 20;
        $pageNo = intval($this->_request->get('pageNo'));
        if (empty($pageNo)) {
            $pageNo = 1;
        }

        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();

        $vars['userName'] = trim($this->_request->get('userName'));
        $vars['name'] = trim($this->_request->get('name'));
        $vars['partnerName'] = trim($this->_request->get('partnerName'));
        $vars['ip'] = trim($this->_request->get('ip'));
        $urls = $vars;

        if ($vars['userName'] != '') {
            $wheres[] = "`u`.`userName` LIKE '%" . addslashes($vars['userName']) . "%'";
        }
        if ($vars['name'] != '') {
            $wheres[] = "`u`.`name` LIKE '%" . addslashes($vars['name']) . "%'";
        }
        if ($vars['partnerName'] != '') {
            if (strpos($vars['partnerName'], '耶客') !== FALSE || strtolower($vars['partnerName']) == 'yek') {
                $wheres[] = "`u`.`type` = 1";
            } else {
                $wheres[] = "`p`.`name` LIKE '%" . addslashes($vars['partnerName']) . "%'";
            }
        }
        if ($vars['ip'] != '') {
            $wheres[] = "`us`.`ip` LIKE '%" . addslashes($vars['ip']) . "%'";
        }
        $orderBy = '`us`.`lastOnlineTime` DESC';

        $urls['pageNo'] = '{page}';
        $urlTemplate = $this->view->projectUrl($urls);

        //获取总页数以及记录
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $sql = "SELECT 
            `us`.*, `u`.`name` AS `name`, `u`.`type`, `p`.`name` AS `partnerName`, `us`.`lastOnlineTime` AS `lastOnlineTime` 
            FROM `{$this->_model->getTableName()}` AS `us` 
            LEFT JOIN `{$userModel->getTableName()}` AS `u` ON `u`.`userName` = `us`.`userName` 
            LEFT JOIN `{$partnerModel->getTableName()}` AS `p` ON `u`.`partnerCode` = `p`.`code` 
            {$where} 
            ORDER BY {$orderBy}
        ";

        $dbPaginator = new Application_Model_DbPaginator($sql, $pageSize, $pageNo);
        $recordCount = $dbPaginator->getRecodCount();

        //获取分页html字符
        Zend_Loader::loadClass('Project_Paginator');
        $paginator = new Project_Paginator($recordCount, $pageSize, $pageNo);
        $paginator->urlTemplateContent = $urlTemplate;
        
        $rows = $dbPaginator->getRows();
        foreach($rows as $key=>$row) {
            $row['isOnline'] = $time - strtotime($row['lastOnlineTime']) > $this->_configs['project']['admin']['effectiveOnlineTime'] ? 0 : 1;
            $rows[$key] = $row;
        }

        //分配view(分页处理后的记录集以及分页HTML字符)
        $this->view->pageString = $paginator->getPageString();
        $this->view->rows = $rows;
        $urls['pageNo'] = $pageNo;
        $this->view->pageUrl = $this->view->projectUrl($urls);
        $this->view->vars = $vars;
    }

    /**
     * 删除动作
     * 
     * @return void
     */
    public function deleteAction()
    {
        if ($this->_request->isPost()) {
            $ids= $this->_request->getPost('selectId');
            if (is_array($ids)) {
                $this->_model->deleteByIds($ids);
            }
        }
        $backUrl = urldecode($this->_request->get('backUrl'));
        redirect($backUrl);
    }
}